package org.ch04.test00.demo04;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author CSY
 * @date 2025 - 03 - 30
 */
public class SqlGenerate {
    /**
     *
     * @param clazz
     * @return
     */
    public static String generateSelect(Class<?> clazz){
        StringBuilder sb = new StringBuilder("select ");
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {
            if (f.isAnnotationPresent(Column.class)){
                Column fieldAnnotation = f.getAnnotation(Column.class);
                String fieldName = fieldAnnotation.value();
                sb.append(fieldName).append(",");
            }else{
                sb.append(f.getName().toLowerCase()).append(",");
            }
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(" from ");
        if (clazz.isAnnotationPresent(Table.class)) {
            Table tableAnnotation = clazz.getAnnotation(Table.class);
            String tableName = tableAnnotation.value();
            sb.append(tableName);
        }else{
            sb.append(clazz.getSimpleName().toLowerCase());
        }
        return sb.toString();
    }
}
